package test.us.versus.them.b3ge.world;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Point;

import hxjson2.JSON;

import us.versus.them.b3ge.world.IsometricWorld;

import us.versus.them.b3ge.Util;
import us.versus.them.b3ge.character.Character;

import us.versus.them.glassifried.Button;
import us.versus.them.glassifried.Glassing;

class TestIsometricWorld {

	public static function main() {
		var clip = flash.Lib.current;
		var status = Util.txt( 'sweet' );

		var worldSprite = new Sprite();
		var world = new IsometricWorld();

		world.parse( 
			'{
				  "version"     : "0.1"

				, "tile_file"   : "ResourcesTilesIsometricGrassie"
				, "obj_height"  : "120" // height from floor to top of image
				, "tile_width"  : "200"
				, "tile_height" : "235" 

				, "map_width"   : "3" // this is really # of tiles horizontally
				, "map_height"  : "3" // this is really # of tiles vertically
				, "map" : [ 
					  [ 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0 ]
					, [ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5 ]
					, [ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5 ]
					, [ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5 ]
					, [ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5 ]
					, [ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5 ]
					, [ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5 ]
					, [ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5 ]
					, [ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5 ]
					, [ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5 ]
					, [ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5 ]
					, [ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5 ]
					, [ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5 ]
					, [ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5 ]
					, [ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5 ]
					, [ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5 ]
					, [ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5 ]
					, [ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5 ]
					, [ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5 ]
					, [ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5 ]
					, [ 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5 ]
					, [ 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0 ]
				]
			}'
		);
		worldSprite.addChild( world );

		// center the world in a 640x480 view...
		// sadly this is all just eyeball'd
		//worldSprite.x = 480 / 2 - world.width / 2  + 70 ;
		//worldSprite.y = 640 / 2 - world.height / 2 + 400 ;

		clip.addChild( worldSprite );

		var johnnie = TestIsometricWorld.addCharacter( clip, world );

		var idx = 0;
		var iOffset = 0;
		var jOffset = 0;

		var tilePickerBitmapData = new BitmapData( world.getTileWidth(), world.getTileHeight() );
		var tilePicker = new Bitmap( tilePickerBitmapData );

		var styleSheet = new flash.text.StyleSheet();
		styleSheet.parseCSS( '
			h3 {
			    font-family: Arial, Helvetica, sans-serif;
			    font-size: 8px;
			    font-weight: 400;
			}
			h4 {
			    font-family: Arial, Helvetica, sans-serif;
			    font-size: 12px;
			    font-weight: 400;
			}
		');
		
		var changeIdx = function( i ) {
			if ( world.offsetOk( i ) ) {
				idx = i;
				status.text = 'idx is ' + idx;
				world.copyPixelsToBitmap( tilePickerBitmapData, idx );
			}
		};

		var previousTile = ' ◀ ';
		var nextTile = ' ▶ ';
		var saveToClipBoard = 'save to clipboard';
		var buttonActions = function( what:String, mouseEvent : MouseEvent ) {
	        switch( what ) {
				case saveToClipBoard: flash.system.System.setClipboard( world.toString() );
				case previousTile: changeIdx( idx - 1 );
				case nextTile: changeIdx( idx + 1 );
				case 'west':  world.supdate( iOffset, --jOffset );
				case 'east':  world.supdate( iOffset, ++jOffset );
				case 'south': world.supdate( ++iOffset, jOffset );
				case 'north': world.supdate( --iOffset, jOffset );
			}

			status.text = world.kookoo;
		};

		var picker = new Sprite();
		picker.x = 10;
		picker.y = 10;
		clip.addChild( picker );
		tilePicker.x = 10;
		world.copyPixelsToBitmap( tilePickerBitmapData, 0 );
		picker.addChild( tilePicker );
		var y = world.getObjectHeight() * 2 / 3;
		picker.addChild( new Button( -5,y, 170,170,238, previousTile, buttonActions, styleSheet ) );
		picker.addChild( 
			new Button( 
				tilePicker.width + 10
				, y
				, 170
				, 170
				, 238
				, nextTile
				, buttonActions
				, styleSheet 
			) 
		);

		worldSprite.addEventListener( 
			MouseEvent.CLICK
			, function( event:MouseEvent ) {
				var clicked = world.tileIndex_( event.localX, event.localY );

				world.mapAtTo( Std.int( clicked.y ), Std.int( clicked.x ), idx );
				world.update();
				return;

				//var clickedAt = world.clickedAt( event.localX, event.localY );
				var clickedAt = world.screenToWorld( new Point( event.localX, event.localY ) );

				status.text = 
					'click:' + Std.int( clickedAt.x ) + ',' + Std.int( clickedAt.y )
					+ ' v/s johnnie:' + 
					Std.int( johnnie.position.x ) + ',' + Std.int( johnnie.position.y )
				;

				johnnie.setGoal( clickedAt );
			}
		);

		// gotta love dynamic languages
		for ( stuff in [
			  { x:10,y:150,name:'ResourcesIconsMapArrowsIsoChevronWest',key:'west' }
			, { x:50,y:180,name:'ResourcesIconsMapArrowsIsoChevronEast',key:'east' }
			, { x:10,y:180,name:'ResourcesIconsMapArrowsIsoChevronSouth',key:'south' }
			, { x:50,y:150,name:'ResourcesIconsMapArrowsIsoChevronNorth',key:'north' }
		] ) {
			clip.addChild(
				Button.wrap( 
					stuff.x, stuff.y
					, 170,170,238
					, Util.displayObjectForName( stuff.name )
					, stuff.key
					, buttonActions
				)
			);
		}

		clip.addChild( new Button( 10,110, 128,0,0, saveToClipBoard, buttonActions, styleSheet ) );

		world.update();

		var overlay = new BitmapData( 640, 480, true, 0x0 );
		clip.addChild( new Bitmap( overlay ) );
		for ( x in 0 ... 640 ) {
			for ( y in 0 ... 480 ) {
//				var clickedAt = world.clickedAt( x, y );
				var clickedAt = world.screenToWorld( new Point( x, y ) );


				clickedAt.x = clickedAt.x / 640 * 255;
				clickedAt.y = clickedAt.y / 480 * 255;
				if ( clickedAt.x < 0 ) clickedAt.x = 0;
				if ( clickedAt.y < 0 ) clickedAt.y = 0;
				if ( clickedAt.x > 255 ) clickedAt.x = 255;
				if ( clickedAt.y > 255 ) clickedAt.y = 255;
				clickedAt.y=0;

				overlay.setPixel32( x, y, 0xCC000000 | Std.int(clickedAt.x) << 16 | Std.int(clickedAt.y) );
			}
		}
		clip.addChild( status );
	}

	public static function addCharacter( clip, world:IsometricWorld ) {
		var johnnie = new Character();
		var a = 2;
		var b = Math.sqrt( a * a * 2 );
		var eljson = ( '{ 
				  "name" : "Johnnie"
				, "actions" : [
					{
						  "name": "Walk"
						, "offsets" : [
							  { "x" :    0, "y": -f2 }
							, { "x" :   f1, "y":  -f1 }
							, { "x" :  f2, "y":    0 }
							, { "x" :   f1, "y":   f1 }
							, { "x" :    0, "y":  f2 }
							, { "x" :  -f1, "y":   f1 }
							, { "x" : -f2, "y":   0  }
							, { "x" :  -f1, "y":  -f1 }
						]
					}
					, { "name": "Die1" }
					, { "name": "Maul" }
				]
			}'
		);
		eljson = ~/f1/g.replace( eljson, '' + a );
		eljson = ~/f2/g.replace( eljson, '' + b );

		johnnie.parse( eljson );
		johnnie.position.x = 00;
		johnnie.position.y = 00;

		johnnie.addEventListener( 
			flash.events.Event.ENTER_FRAME
			, function( _ ) { 
				johnnie.move();
				world.update();
			} 
		)
		;
		world.addCharacter( johnnie );
		clip.addChild( johnnie );
		return johnnie;
	}
}
